package me.guizhi.work.web;

import me.guizhi.work.domain.User;
import me.guizhi.work.service.UserImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

@WebServlet(name = "AddServlet", value = "/addServlet")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        Integer id = Integer.parseInt(request.getParameter("id"));
        String username = request.getParameter("name");
        String pwd = request.getParameter("password");
        Integer sex = Integer.parseInt(request.getParameter("sex"));
        Date birthday = java.sql.Date.valueOf(request.getParameter("birthday"));

        response.setCharacterEncoding("utf8");

        response.getWriter().println("uname:"+username);
        response.getWriter().println("pwd:"+pwd);

        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(pwd);
        user.setSex(sex);
        user.setBirthday(birthday);
        try {
            new UserImpl().addUser(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response.getWriter().println("添加成功");
    }
}
